package com.shiwaixiangcun.customer.module.heath

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.google.gson.reflect.TypeToken
import com.jaeger.recyclerviewdivider.RecyclerViewDivider
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.ConfigSps
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.GlobalConfig
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.AdapterEvaluationRecord
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.network.ResponseConfig
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr
import com.shiwaixiangcun.customer.utils.JsonUtil
import com.shiwaixiangcun.customer.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.activity_evaluation_record.*
import kotlinx.android.synthetic.main.top_bar_white.*

/**
 *
 * 健康测评历史
 */
class EvaluationRecordActivity : BaseActivity(), View.OnClickListener {

    private var refreshToken: String? = null
    private var tokenString: String? = null


    private var mAdapter: AdapterEvaluationRecord? = null
    private var mDataList: MutableList<EvaluationData>? = null
    var siteId: Int = 20
    private var pageStart: Int = 1
    var pageSize: Int = 15

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_evaluation_record)
        initViewAndEvent()

        initData(false)
    }

    private fun initData(isLoadMore: Boolean) {


        OkGo.get<String>(GlobalApi.EVALUATIONI_RECORD)
                .params("page.pn", pageStart)
                .params("page.size", pageSize)
                .params("access_token", tokenString)
                .execute(object : StringCallback() {
                    override fun onError(response: Response<String>?) {
                        super.onError(response)
//                        Log.e(BUG_TAG, "error")
                        mAdapter?.setEnableLoadMore(true)
                        mAdapter?.loadMoreFail()
                        refreshLayout.finishRefresh()
                    }

                    override fun onSuccess(response: Response<String>?) {
                        val type = object : TypeToken<CommonEntity<Evaluation>>() {}.type

                        val responseData = JsonUtil.fromJson<CommonEntity<Evaluation>>(response?.body(), type)
                        when (responseData.responseCode) {
                            ResponseConfig.SUCCESS -> {
                                val size = responseData.data.elements.size
                                if (isLoadMore) {
                                    if (size > 0) {
                                        mDataList?.addAll(responseData.data.elements)
                                        mAdapter?.notifyDataSetChanged()
                                    }
                                    if (size == 0 || size < pageSize) {
                                        mAdapter?.loadMoreEnd(false)
                                    } else {
                                        mAdapter?.loadMoreComplete()
                                    }
                                } else {
                                    refreshLayout.finishRefresh()
                                    mAdapter?.setEnableLoadMore(true)
                                    if (size == 0) {
//                                        clayout_nodata.visibility = View.VISIBLE
                                    } else {
                                        mDataList?.clear()
                                        mDataList?.addAll(responseData.data.elements)
                                        mAdapter?.notifyDataSetChanged()
                                    }
                                }
                            }
                        }

                    }
                })
    }

    private fun initViewAndEvent() {

        tv_page_name.text = "历史测评"
        mDataList = ArrayList()
        back_left.setOnClickListener(this)
        refreshToken = AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_REFRESH_TOKEN, "") as String
        tokenString = AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_TOKEN, "") as String
        pageStart = GlobalConfig.first_page
        pageSize = GlobalConfig.page_size
        mAdapter = AdapterEvaluationRecord(mDataList)
        rv_evaluation_record.layoutManager = LinearLayoutManager(mContext)
        rv_evaluation_record.adapter = mAdapter
        val divider = RecyclerViewDivider.Builder(mContext)
                .setOrientation(RecyclerViewDivider.VERTICAL)
                .setStyle(RecyclerViewDivider.Style.BETWEEN)
                .setMarginLeft(80f)
                .setSize(0.5f)
                .setColorRes(R.color.color_divider_0_1)
                .build()
        rv_evaluation_record.addItemDecoration(divider)
        mAdapter?.setOnItemClickListener { adapter, view, position ->
            val bean = adapter.data[position] as EvaluationData
            val bundle = Bundle()
            bundle.putString("evaluationType", bean.assessmentType)
            bundle.putLong("evaluationId", bean.id)
            readyGo(EvaluationDetailActivity::class.java, bundle)
        }

        mAdapter?.setLoadMoreView(CustomLoadMoreView())
        mAdapter?.setOnLoadMoreListener({
            pageStart += 1
            initData(true)
        }, rv_evaluation_record)

        refreshLayout.setOnRefreshListener {
            pageStart = 1
            initData(false)
            mAdapter?.setEnableLoadMore(false)
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.back_left -> finish()

            else -> {
            }
        }

    }
}